<?php

namespace common\models\member;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%member_level}}".
 *
 * @property int $id
 * @property int $type_id 会员类型ID
 * @property string $name 等级名称
 * @property int $level_no 等级编号
 * @property string $icon 等级图标
 * @property string $thumb 缩略图
 * @property string $description 特权介绍
 * @property int $min 最小经验
 * @property int $max 最大经验
 * @property string $scale 折扣比例
 * @property int $status 启用状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class MemberLevel extends \common\db\ActiveRecord {

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%member_level}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['type_id', 'level_no', 'min', 'max', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name'], 'required'],
            [['scale'], 'number'],
            [['name'], 'string', 'max' => 50],
            [['icon', 'thumb', 'description'], 'string', 'max' => 255],
            [['level_no', 'min', 'max'], 'default', 'value' => 0],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'type_id' => '会员类型ID',
            'name' => '等级名称',
            'level_no' => '等级编号',
            'icon' => '等级图标',
            'thumb' => '缩略图',
            'description' => '特权介绍',
            'min' => '最小经验',
            'max' => '最大经验',
            'scale' => '折扣比例',
            'status' => '启用状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    public function getLevelIcon() {
        return Url::attach($this->icon);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'name',
            'level_no',
            'description',
            'icon' => 'levelIcon',
            'frame' => function() {
                return Url::attach($this->thumb);
            },
        ];
    }

}
